<?php
/**
 * <pre>
 * <b>RUBIK IT Solution & Development ltd.</b>
 *
 * PROJECT		: Auction
 * @version 	: 1.1
 * COPYRIGHT    : 2010
 * ------------------------------------------------------
 * @author		: haunhp <haunhp@rubik.com.vn>
 * Created on	:  Sep 13, 2010
 * </pre>
 *
 */
class AuctionHelper {
	/**
	 * Get auctions by Owner ID
	 *
	 * @param int $ownerId: Owner ID
	 * @param int $RECORD_PER_PAGE: Number of records per page (used for pagination)
	 * @param int $page: Page index
	 * @return SelectResult
	 */
	static function getAuctionsByOwner($ownerId, $RECORD_PER_PAGE = 12, $page = 1, $extWhere = '') {
		$sql = "SELECT a.*, v.thumbnail, v.year, v.odometer, m.name AS maker, md.model_group, s.name AS state_name, c.name AS city_name, cy.name AS country_name
				FROM auctions a 
				LEFT JOIN vehicle v ON a.vehicle_id = v.vehicle_id 
				LEFT JOIN maker m ON v.maker_id = m.maker_id 
				LEFT JOIN model md ON v.model_id = md.model_id 
				LEFT JOIN state s ON a.state_id = s.state_id 
				LEFT JOIN city c ON a.city_id = c.city_id 
				LEFT JOIN country cy ON a.country_id = cy.id 
				WHERE a.owner_id = '{$ownerId}' AND a.deleted = 0 $extWhere  ORDER BY a.auction_id DESC";
		return SysDatabase::getSelectResult($sql, $RECORD_PER_PAGE, $page);
	}

	/**
	 *
	 * Get bidding auctions of buyer
	 * @param int $bidderId: Buyer ID
	 * @param int $RECORD_PER_PAGE: Number of records per page
	 * @param int $page: Page index
	 * @param string $extWhere: External WHERE clause
	 * @return SelectResult
	 */
	static function getBiddingAuctions($bidderId, $RECORD_PER_PAGE = 12, $page = 1, $extWhere = '') {
		$sql = "SELECT `b`.`id`, `b`.`auction_id` AS `auid`, `b`.`bid_amount`, `b`.`bid_status`, `b`.`date_bid`, `u`.`business_name`,
			`a`.*, `v`.`thumbnail`, `v`.`year`, `v`.`odometer`, `m`.`name` AS `maker`, `md`.`model_group`, 
			`s`.`name` AS `state_name`, `c`.`name` AS `city_name`, `cy`.`name` AS `country_name` 
			FROM (SELECT * FROM `bids` ORDER BY `id` DESC) `b` 
			LEFT JOIN `auctions` `a` ON `b`.`auction_id` = `a`.`auction_id` 
			LEFT JOIN `user` `u` ON `a`.`owner_id` = `u`.`user_id` 
			LEFT JOIN `vehicle` `v` ON `a`.`vehicle_id` = `v`.`vehicle_id` 
			LEFT JOIN `maker` `m` ON `v`.`maker_id` = `m`.`maker_id` 
			LEFT JOIN `model` `md` ON `v`.`model_id` = `md`.`model_id` 
			LEFT JOIN `state` `s` ON `a`.`state_id` = `s`.`state_id` 
			LEFT JOIN `city` `c` ON `a`.`city_id` = `c`.`city_id` 
			LEFT JOIN `country` `cy` ON `a`.`country_id` = `cy`.`id` 
			WHERE `b`.`user_id` = '$bidderId' $extWhere 
			GROUP BY `b`.`auction_id`";
		return SysDatabase::getSelectResult($sql, $RECORD_PER_PAGE, $page);
	}

	/**
	 *
	 * Get bids count of an auction
	 * @param int $aucId: Auction ID
	 * @return number on success, or False on failure
	 */
	static function getBidsCount($aucId) {
		$sql = "SELECT COUNT(*) FROM bids WHERE auction_id = '{$aucId}'";
		return SysDatabase::loadResult($sql);
	}

	/**
	 *
	 * Get bidder count of an auction
	 * @param int $aucId: Auction ID
	 * @return number on success, or False on failure
	 */
	static function getBidderCount($aucId) {
		$sql = "SELECT COUNT(DISTINCT(user_id)) FROM bids
				WHERE auction_id = '{$aucId}'";
		return SysDatabase::loadResult($sql);
	}

	/**
	 *
	 * Get owner name
	 * @param int $id: Owner ID
	 * @return string: Owner business name
	 */
	static function getNameOwner($id){
		$sQuery	= "SELECT `business_name` FROM `user` WHERE `user_id` = '$id'";
		$rs = SysDatabase::loadObject($sQuery);
		return $rs->business_name;
	}

	/**
	 *
	 * Get maker of a vehicle
	 * @param int $id: Vehicle ID
	 * @return string: maker name
	 */
	static function getMaker($id){
		$sQuery	= "SELECT * FROM `maker` WHERE `maker_id` = '$id'";
		$rs = SysDatabase::loadObject($sQuery);
		return $rs->name;
	}

	/**
	 *
	 * Get model of a vehicle
	 * @param int $id: Model ID
	 * @return string: Model group name
	 */
	static function getModel($id){
		$sQuery	= "SELECT * FROM `model` WHERE `model_id` = '$id'";
		$rs = SysDatabase::loadObject($sQuery);
		return $rs->model_group;
	}

	/**
	 *
	 * Get vehicle photo from gallery
	 * @param int $id: Photo ID
	 * @return PhotoDTO
	 */
	static function getPhotoFromGallery($id){
		RClassesImporter::cls('dbo.dtos.PhotoDTO');
		$my_Obj = new PhotoDTO();
		$my_Obj->setKeyValue($id);
		SysDatabase::fillData($my_Obj);
		return $my_Obj;
	}

	/**
	 * 
	 * Get setting by ID
	 * @param int $id: Setting ID
	 * @return object
	 */
	static function getSettingById($id) {
		$sql = "SELECT * FROM general_setting WHERE id = '{$id}'";
		return SysDatabase::loadObject($sql);
	}
}
